package bb.lanxing.device.thirdpart.gpx

import bb.lanxing.lib.kt.model.geo.IGeoPoint
import bb.lanxing.model.map.GeoPoint
import io.ticofab.androidgpxparser.parser.domain.Gpx

object GPXextKt {
    fun isValid(gpx: Gpx): Boolean {
        val routes = gpx.routes
        if (!(routes != null && (routes.isNotEmpty()))) {
            val tracks = gpx.tracks
            if (!(tracks != null && (tracks.isNotEmpty()))) {
                return false
            }
        }
        return true
    }

    fun getPoints(gpx: Gpx): List<List<IGeoPoint>> {
        val routes = gpx.routes
        var result = false
        if (routes != null && (routes.isNotEmpty())) {
            val arrayList: ArrayList<List<IGeoPoint>> = ArrayList(routes.size)
            for (route in routes) {
                val routePoints = route.routePoints
                val arrayListInner: ArrayList<IGeoPoint> = ArrayList(
                    routePoints.size
                )
                for (routePoint in routePoints) {
                    val latitude = routePoint.latitude
                    val longitude = routePoint.longitude
                    arrayListInner.add(GeoPoint(0, latitude, longitude))
                }
                arrayList.add(arrayListInner)
            }
            return arrayList
        }
        val tracks = gpx.tracks
        if (tracks != null && (tracks.isNotEmpty())) {
            result = true
        }
        if (result) {
            val arrayList: ArrayList<List<IGeoPoint>> = ArrayList()
            for (track in tracks) {
                val trackSegments = track.trackSegments
                if (null != trackSegments && (trackSegments.isNotEmpty())) {
                    val arrayListInner: ArrayList<List<IGeoPoint>> = ArrayList()
                    for (trackSegment in trackSegments) {
                        val trackPoints = trackSegment.trackPoints
                        val arrayList5 = ArrayList<IGeoPoint>()
                        if (null != trackPoints && (trackPoints.isNotEmpty())) {
                            for (trackPoint in trackPoints) {
                                val latitude2 = trackPoint.latitude
                                val longitude2 = trackPoint.longitude
                                arrayList5.add(GeoPoint(0, latitude2, longitude2))
                            }
                            arrayListInner.add(arrayList5)
                        }
                        arrayList.addAll(arrayListInner)
                    }
                }
            }
            return arrayList
        }
        return emptyList()
    }
}